/*
 * =====================================================================================
 *
 *       Filename:  LangDetector.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015年04月01日 20时18分50秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  LiuYi (), swliuyi@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include "LangDetector.h"
using namespace std;
LangDetector::LangDetector(string langName, string code, string charSetPath, string LMPath, int maxGramNum):m_langName(langName), m_code(code), m_pLM(NULL)
{
    m_pLM = new WordLM(LMPath, maxGramNum);
    if( m_pLM == NULL )
    {
        cerr <<"load LM :"<<LMPath<<"error"<<endl;
        return;
    }
    bool flag = LoadCharSet(charSetPath);
    if( flag == 0 )
    {
        cerr<<"load charSet :"<<charSetPath<<"error"<<endl;
        return;
    }
}

bool LangDetector::LoadCharSet(string charSetPath)
{
    ifstream fin(charSetPath.c_str());
    if(!fin)
    {
        return false;
    }
    string line;
    while(getline(fin, line))
    {
        cout<<line<<endl;
        m_CharSet.insert(line);
    }
    fin.close();
    return true;
}

double LangDetector::ComputeCharScore(vector<string> sent)
{
    double score = 0;
    for( int i = 0 ; i < sent.size() ; i++ )
    {
        if( sent[i] != " " && m_CharSet.count(sent[i]) != 0)
        {
            score += 1;
        }
    }
    return score/sent.size();
}

double LangDetector::ComputeLMScore(string sent, int gramNum)
{
    return m_pLM->ComputeLMScore(sent, gramNum);
}

LangDetector::~LangDetector()
{
    delete m_pLM;
}
